<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>菜单编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="/web/res/css/font.css">
        <link rel="stylesheet" href="/web/res/css/xadmin.css">
        <script src="/web/res/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="/web/res/js/xadmin.js"></script>
</head>

<body style="background-color: white;">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend class="form-title"><div id="tip">【${menu.title!''}】</div></legend>
    </fieldset>
    <div class="layui-card-header">
        <button class="layui-btn layui-btn-danger layui-btn-sm addMeNu">
            <i class="layui-icon layui-icon-add-1"></i>添加下级
        </button>
        <a class="layui-btn layui-btn-small lable-text-center"
           style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()"
           title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>刷新表格数据</a>
    </div>
    <#if menu.pid?exists && (menu.pid)?number != -1></#if>
    <div class="layui-fluid" id="orderEdit">
        <form class="layui-form layui-form-pane"  id="formsubmit"  action="/menu/save">
            <input type="hidden" name="id" id="id" value="${(menu.id?c)!''}" />
            <input type="hidden" name="pid" id="pid" value="${(menu.pid?c)!''}" />
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">菜单名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="title" value="${(menu.title)!''}" placeholder="菜单名称" autocomplete="off" class="layui-input" required lay-verify="required" >
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">菜单地址</label>
                    <div class="layui-input-inline">
                        <input type="text" name="links" value="${menu.links!''}"  placeholder="菜单地址" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">排序值</label>
                    <div class="layui-input-inline">
                        <input type="text" name="sorts" required lay-verify="required|number" autocomplete="off" class="layui-input" value="${(menu.sorts?c)!''}">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">图标</label>
                    <div class="layui-input-inline">
                        <input type="text" name="icon" value=""  placeholder="菜单图标" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux" id="Icon" style="cursor:pointer;"> <i title="点击更多图标" class="iconfont">${menu.icon!''}  图标链接</i></div> 
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label lable-text-center">是否有效</label>
                    <div class="layui-input-inline">
                        <input type="checkbox" name="states" value="1" lay-skin="switch" lay-text="启用|不启用"  <#if menu.states?exists && (menu.states)?number == 1>checked</#if>/>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">菜单接口</label>
                    <div class="layui-input-inline">
                        <button type="button" class="layui-btn layui-btn-sm addApi" id="LAY-component-form-setval" style="margin-left: 10px">添加接口</button>
                    </div>
                </div>
            </div>

            <div class="layui-form-item" api  style="display:none;">
                <div class="layui-inline">
                    <label class="layui-form-label">接口名
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="apititle" name="apititle" placeholder="请输入" class="layui-input" value="">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">接口地址</label>
                    <div class="layui-input-inline">
                        <input type="text" id="apilinks" name="apilinks" placeholder="请输入" class="layui-input" value="">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-btn layui-btn-sm layui-btn-danger delApi" >删除</label>
                </div>
            </div>

            <div apilist>
                <#if menu.elementList?exists>
                <#list menu.elementList as element>

                <div class="layui-form-item" api>
                    <input type="hidden" id="apiid"  value="${element.id}" />
                    <div class="layui-inline">
                        <label class="layui-form-label">接口名
                        </label>
                        <div class="layui-input-inline">
                            <input type="text" id="apititle" name="apititle" placeholder="请输入" class="layui-input" value="${element.title}">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">接口地址</label>
                        <div class="layui-input-inline">
                            <input type="text" id="apilinks" name="apilinks" placeholder="请输入" class="layui-input" value="${element.links}">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-btn layui-btn-sm layui-btn-danger delApi" >删除</label>
                    </div>
                </div>
                </#list>
                </#if>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注信息</label>
                <div class="layui-input-block">
                    <textarea name="comments" class="layui-textarea">${menu.comments!''}</textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn layui-btn-radius" lay-submit lay-filter="save">&emsp;保存&emsp;</button>
                    <button type="button" class="layui-btn layui-btn-primary layui-btn-radius delmenu" id="delmenu" >&emsp;删除&emsp;</button>
                </div>
            </div>
        </form>
    </div>

    <script>
        layui.use('form', function () {
            let $ = layui.$;
            $.fn.extend({
                serializeObject: function() {
                    var obj = {}; //声明一个对象
                    $.each(this.serializeArray(), function(index, field) {
                        obj[field.name] = field.value; //通过变量，将属性值，属性一起放到对象中
                    })
                    return obj;
                }
            });
            var form = layui.form;
            form.render();

            $('#Icon').on('click',function(){
                window.open('http://x.xuebingsi.com/x-admin/v2.2/', '_blank') 
            })
            
            // 添加Api接口
            $('.layui-form-item').on('click', '.addApi', function () {
                var strVar=$("div[api]:eq(0)").clone(true); //克隆元素,注意不是javascript的cloneNode()
                strVar.attr("style","display:block;");
                $("div[apilist]").after(strVar);
                return ;
            });

            // 删除Api接口
            $('.layui-form-item').on('click', '.delApi', function () {
                $(this).parents(".layui-form-item").remove();
                return ;
            });

            // 添加下级菜单
            $('.layui-card-header').on('click', '.addMeNu', function () {
                var pid = $("#id").val();
                $(':input','#formsubmit')
                    .not(':button, :submit, :reset,:checkbox')
                    .val('');
                $('#pid').val(pid);
                $('#delmenu').remove();

                $('div[api]').each(function(index,ele){
                    // 先获取内部元素
                    if(index!=0){
                        ele.remove();
                    }
                });
                $('#tip').text("正在添加 "+$('#tip').text()+" 的下级菜单");

                return false;
            });


            // 删除菜单
            $('.layui-form-item').on('click', '.delmenu', function () {
                var id = $("#id").val();
                layer.confirm('确认要删除吗？', function (index) {
                    ajaxPost($,"/menu/del",{"id":id},function (data) {
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 1000}, function(){
                                window.parent.freshref(${menu.pid});
                            });
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 1000}, function(){

                            });
                        }
                    });
                });
            });

            form.on('submit('+$("button[lay-submit]:eq(0)").attr("lay-filter")+')', function(data){
                $("div[api]:eq(0)").remove();  //删除隐藏的API元素

                $('div[api]').each(function(index,ele){
                    // 先获取内部元素
                    $(this).find("input[id='apiid']").attr("name","elementList["+index+"].id");
                    $(this).find("input[id='apititle']").attr("name","elementList["+index+"].title");
                    $(this).find("input[id='apilinks']").attr("name","elementList["+index+"].links");
                });

                layer.confirm('确认要保存吗？', function (index) {
                    ajaxPost($,$("#formsubmit").attr("action"),$("#formsubmit").serializeObject(),function (data) {
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 1000}, function(){
                                window.parent.freshref(${(menu.id?c)!''});
                            });
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 1000}, function(){
                                xadmin.close();
                            });
                        }
                    });
                });
            });
        });
    </script>
</body>

</html>